package tool;

import javax.microedition.media.Control;
import javax.microedition.media.Player;
import javax.microedition.media.control.VolumeControl;

import state.State;

import boot.Boot;

public class PlayerUtils {

	public static final int MAX_VOLUME = 100;
	public static final int SOURDINE_VOLUME = 20;
	
	public static VolumeControl getVolumeControl(Player aPlayer) {
        Control[] controls = aPlayer.getControls();
        VolumeControl volumeControl = null;
        
        for (int i = 0; i < controls.length; i++) {
            if (controls[i] instanceof VolumeControl) {            	
            	volumeControl = (VolumeControl) controls[i];
            }
        }
        return volumeControl;
	}

	public static int getVolumeValue(int nightModeIndex) {
		
		int maxVolume = getCurrentMaxVolume();
		
		// Index mode varying from 5 to 1
		int[] volumes = new int[] {5, 10, 20, 30, 60};
		return volumes[nightModeIndex - 1] * maxVolume / MAX_VOLUME;
	}
	
	public static void setMaxVolume(Player aPlayer) {
		VolumeControl volumeControl = PlayerUtils.getVolumeControl(aPlayer);
		int volume = getCurrentMaxVolume();
		if (volumeControl != null) {
			volumeControl.setLevel(volume);
		}
	}

	public static int getCurrentMaxVolume() {
		int value = MAX_VOLUME;
		if (Boot.state().equals(State.SOURDINE)) {
			value = SOURDINE_VOLUME;
		}
		return value;
	}
	
}
